var sesspersonnel= JSON.parse(sessionStorage.info_progect||'{}')
if(sesspersonnel.checkInTime){
	sesspersonnel.qiandaoTime="签到时间:"+sesspersonnel.checkInTime;
	sesspersonnel.qiandao="签退";
	sesspersonnel.qiandaonum=1;
}else{
	sesspersonnel.qiandaoTime="签到时间：";
	sesspersonnel.qiandao="签到";
	sesspersonnel.qiandaonum=0;	
}

var main = new Vue({
	el: '#main',
	data: {
		personnel: sesspersonnel,
		assessResponse: [],//题目数组
		Popupshow:false,//弹窗是否显示
		assessnum:0,//第几题
		assessstyle:0,//答题状态0未完成答题1完成答题2上传成功3上传失败
		objanswer:null,//储存上传失败的问题答案
		ip:localStorage.ip,
	},
	computed: {
		completed:function(){
			var hasnum = 0;
			for (var i=0;i<this.assessResponse.length;i++){
				if(this.assessResponse[i].assessId > 0){
					hasnum++
				}
			}
			return hasnum
		}
	},
	methods: {
		staging:function(){
			this.Popupshow=false;
			localStorage.objanswer=this.objanswer;
		},
		pinggu:function(num){
			if(this.personnel.qiandaonum==0){
				log_message("请先签到")
				return false;
			}
			this.Popupshow=true;
		},
		closepinggu:function(styless){
			this.Popupshow=false;
			if(styless == 'true'){
//				window.history.go(-1);
			}
		},
		choose:function(choosenum){
			this.assessResponse[this.assessnum].assessId = choosenum+1;
		},
		nextassess:function(){
			if(this.assessnum<this.assessResponse.length-1){
				this.assessnum+=1;
			} 
		},
		opennum:function(num){
			if(this.personnel.qiandaonum==0){
				log_message("请先签到")
				return false;
			}
			if(!this.personnel.score){
				this.Popupshow = true;
				this.assessnum = num;
			}
			
		},
		tijiao:function(){
			if(this.completed == this.assessResponse.length){
				this.assessstyle=1;
			}else{
				log_message("还有未填写的");
			}
		},
		qiandaofun:function(){
			this.personnel.qiandaonum +=1;
			if(this.personnel.qiandaonum == 1){
				this.personnel.qiandao=""
				this.personnel.qiandaoTime= "签到时间："+ new Date().toLocaleString()
				checkinajax()
			}else{
				if(!this.personnel.score && this.completed != this.assessResponse.length){
					log_message("请先填写评估表！");
					return;
				}
				checkoutajax()
				this.personnel.qiandao="";
				this.personnel.qiandaoTime= "签退时间："+ new Date().toLocaleString()
			}
		},
		submitchoose:function(){
			var arranswers=[];
			for (var i=0;i<this.assessResponse.length;i++) {
				arranswers.push(
					{
						assessPosition:this.assessResponse[i].assessPosition,
						assessId:this.assessResponse[i].assesslist[this.assessResponse[i].assessId-1].assessContentId
					}
				)
			}
			console.log(arranswers)
			submitanswer(arranswers)
		}
	}
})
//问题列表
function questionList(){
	ajaxGet(
		Url.ASSESS_QUESTION,
		{
			"nursingId":JSON.parse(sessionStorage.info_progect).nursingId
		},
		function(data){
			console.log(data)
			if(data.status==1){
				main.assessResponse=data.response;
			}
			document.getElementById("t_body").setAttribute("style","background: #698af3;");
			document.getElementById("main").setAttribute("style","display: block;");
		}
	)
}
questionList()
//提交选题答案
function submitanswer(answer){
	var objanswer={};
	objanswer.choose=answer;
	objanswer.nursingId=JSON.parse(sessionStorage.info_progect).nursingId;
	console.log(objanswer)
	ajaxPostJson(
		Url.ASSESS_SAVASSESS,
		objanswer,
		function(data){
			console.log(data)
			if(data.status==1){
				main.Popupshow=true;
				main.assessstyle=2;
				checkoutajax();
			}else{
				log_message(data.message)
			}
		},
		function(){
			main.Popupshow=true;
			main.assessstyle=3;
			main.objanswer=JSON.stringify(objanswer)
		}
	)
}
//签到
function checkinajax(){
	ajaxPost(
		Url.ASSESS_SMPCHECKIN,
		{
			"nursingId":sesspersonnel.nursingId
		},
		function(data){
			console.log(data)
			if(data.status == "1"){
				log_message("签到成功")
			}
		}
	)
}
function checkoutajax(){
	ajaxPost(
		Url.ASSESS_SMPCHECKOUT,
		{
			"nursingId":sesspersonnel.nursingId
		},
		function(data){
			console.log(data)
			if(data.status == "1"){
				main.personnel.qiandaoTime= "签退时间："+ new Date().toLocaleString()
				log_message("签退成功")
			}
		}
	)
}